<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>我的商品</title>
    <link href="css/bootstrap.css" rel="stylesheet">
    <script src="js/jquery-3.6.4.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/popper.js/2.9.3/umd/popper.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
</head>
<body>
<div id="top"></div>
<div class="panel panel-default" style="margin: 5% auto;width: 95%;">
    <div class="panel-heading">
        <h3 class="panel-title"><span class="glyphicon glyphicon-equalizer"></span>&nbsp;&nbsp;我的商品</h3>
    </div>
    <div class="panel-body">
        <table class="table table-striped table-bordered table-hover">
            <tr>
                <td colspan="3">
                    <table class="table table-striped table-bordered table-hover">
                        <tr class="info">
                            <th>序号</th>
                            <th>商品封面</th>
                            <th>商品名称</th>
                            <th>商品单价</th>
                            <th>商品信息</th>
                            <th>商品库存</th>
                            <th>商品销量</th>
                            <th>商品评分</th>
                            <th>操作</th>
                        </tr>
                        <tbody id="product-part"></tbody>
                    </table>
                </td>
            </tr>
        </table>
    </div>
</div>

<script type="text/javascript">
    $(function () {
        $("#top").load("top2.html")
        //请求数据渲染页面
        $.ajax({
            url: "/PDD/product?method=getProductByUid",
            dataType: "json",
            type: "POST",
            success: function (data) {
                let products = data.data
                for (let i = 1; i <= products.length; i++) {
                    let product = products[i - 1]
                    let html2 = `<tr>
  <th>${i}</th>
  <th>
    <img src="image/${product.pImage}" width="50px" height="50px">
  </th>
  <th>${product.pName}</th>
  <th>${product.price.toFixed(2)}</th>
  <th>${product.pInfo}</th>
  <th>${product.stock}</th>
  <th>${product.sales}</th>
  <th>${product.pScore.toFixed(2)}</th>
  <th>
    <button id="modify${i}" type="button" class="btn btn-danger" data-toggle="modal" data-target="#exampleModal">修改</button>
    <a href="sendDiscount.html?pId=${product.pId}" type="button" class="btn btn-danger">发放优惠劵</a>
    <a href="timetask.html?pId=${product.pId}" type="button" class="btn btn-danger">定时修改</a>
  </th>
</tr>`
                    $("#product-part").append(html2)
                    //修改按钮绑定点击事件
                    $("#modify"+i).click(function (){
                        //跳转到修改页面 把pid传过去
                        window.location.href="/PDD/modifyProduct.html?pId="+product.pId
                    })
                }
            }
        })

    })
</script>
</body>
</html>